const Router = require('koa-router');
const router = new Router();
const {db, QueryTypes} = require("../../models/db");

// 通过ID查询赠送信息
router.post('/bill/getSendInfoById', async ctx => {
    let user = ctx.user
    let body = ctx.request.body
    let tenantId = user.tenantId;
    let id = body.id;
    if (id === "合同") {
        return ctx.err('');
    } else {
        let sql = `
            SELECT a.id,a.tenantId,a.buId,b.name buName,a.invId,c.name invName,a.sendInvId,d.name sendInvName,a.buyQty,a.sendQty,
                   d.spec,
                   iif(iif(a.sendUnit is Null,d.unit1,a.sendUnit)=d.unit1,d.spec,1) calNum,
                   iif(a.sendUnit is NULL,d.unit1,a.sendUnit) unit1,d.unit2,a.isDel,a.ctime from send a INNER JOIN contact b on a.buId=b.id
                                                                      INNER join v_goods as c on a.invId=c.id inner join v_goods d on a.sendInvId=d.id
            where a.tenantId=${tenantId} and a.isDel=0 and a.id=${id}
        `
        let ret = await db.query(sql, { type: QueryTypes.SELECT , plain: true})
        if (ret) {
            return ctx.suc('', ret);
        }
        return ctx.err('');
    }

})

module.exports = router;